<?php
namespace Home\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: li gui gong
 * Date: 2018/10/8
 * Time: 14:02
 */
class UpdatedataController extends Controller
{
    public function jiachuju()
    {
        $arr = array(array('name' => '京A4563', 'age' => 12), array('name' => 'lisi', 'age' => 22), array('name' => '京A45636', 'age' => 72), array('name' => 'lisi', 'age' => 58), array('name' => 'wangwu', 'age' => 56), array('name' => 'wangwu', 'age' => 56));
        echo json_encode($arr);
    }

    /**
     * @param $database  要更新的数据库
     * @param $url  接口地址
     * @param array $postData  要传递的post参数
     */
    public function updateChe($postData=array())
    {
        $result = json_decode(send_post('http://www.mifeng.com/index.php/Home/Updatedata/jiachuju',$postData), true);
        //先插入再更新
        //dump($zhi);
        $dataList = array();
        foreach ($result as $v) {
            //组装数组
            $dataList[] = array('che_number' => $v['name'], 'type' => $v['age']);
        }
        //数据的插入和更新
        M('Ceshi')->where('1')->delete();   //先删除所有的数据
        $res = M('Ceshi')->addAll($dataList,$options=array(),$replace=true);
        if(!$res){
            $this->error('数据同步失败');
        }
    }
}









